import { Inject, Injectable } from '@nestjs/common';
import { createTransport, Transporter } from 'nodemailer';
import { ConfigService } from '@nestjs/config';

@Injectable()
export class EmailService {
  transporter: Transporter;

  constructor(private readonly configService: ConfigService) {
    const emailConfig = this.configService.get('email');
    this.transporter = createTransport({
      host: emailConfig?.host,
      port: emailConfig?.port,
      secure: false,
      auth: {
        user: emailConfig?.user,
        pass: emailConfig?.auth,
      },
    });
  }
  async sendMail({ to, subject, html }) {
    const sendMailRes = await this.transporter.sendMail({
      from: {
        name: '系统邮件',
        address: this.configService.get('email.user'),
      },
      to,
      subject,
      html,
    });
    console.log('=======sendMailRes========');
    console.log(sendMailRes);
    return '验证码发送成功';
  }
}
